package com.example.e_comm.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.example.e_comm.entity.Permission;
import com.example.e_comm.service.PermissionService;
import com.example.e_comm.service.UserService;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Set;

@RestController
@RequestMapping("/api/permission")
public class PermissionController {
    private final PermissionService permissionService;
    private UserService userService;
    public PermissionController(PermissionService _permissionService, UserService _userService) {
        this.permissionService = _permissionService;
        this.userService = _userService;
    }
//    public PermissionController(PermissionService permissionService) {
//        this.permissionService = permissionService;
//    }
    //获取所有权限
    @GetMapping("/list")
    public List<Permission> list() {
        return permissionService.list();
    }
    @GetMapping("/listByPage")
    public IPage<Permission> listByPage(
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize
    ){
        return permissionService.page(new Page<>(pageNum,pageSize));
    }
    //获取分页用户信息
    @GetMapping("/listByPageWithCondition")
    public IPage<Permission> listByPageWithCondition(
        @RequestParam(defaultValue = "1") Integer pageNum,
        @RequestParam(defaultValue = "10") Integer pageSize,
        @RequestParam(required = false) String keyword
    ){
        Page<Permission> page = new Page<>(pageNum,pageSize);
        return permissionService.getPermissionsByPageWithCondition(page,keyword);
    }
//    通过用户ID获取权限列表:
    @GetMapping("/{userId}")
    public List<Permission> getUserPermissions(@PathVariable Long userId) {
        return permissionService.getPermissionsByUserId(userId);
    }
//    通过用户ID获取权限编码集合
    @GetMapping("/user/{userId}/codes")
    public Set<String> getUserPermissionCodes(@PathVariable Long userId) {
        return permissionService.getPermissionCodesByUserId(userId);
    }

    @PostMapping("/create")
    public boolean create(@RequestBody Permission permission) {
        if (permissionService.count(
                new QueryWrapper<Permission>()
                        .eq("perm_name", permission.getPermName())) > 0) {
            return false;
        }
        if (permissionService.count(
                new QueryWrapper<Permission>()
                        .eq("perm_key", permission.getPermKey())) > 0) {
            return false;
        }
        return permissionService.save(permission);
    }
    /**
     * 根据用户获取其权限
     * @param userId
     * @return 权限列表
     */
    @GetMapping("/listPermissionByUserId")
    public List<Permission> getPermissionByUserId(@RequestParam Long userId) {
        if(permissionService.getById(userId)==null){
            throw new RuntimeException("用户不存在");
        }
        return permissionService.getPermissionsByUserId(userId);
    }
    /**
     * 根据角色获取其权限
     * @param roleId
     * @return 权限列表
     */
    @GetMapping("/listPermissionByRoleId")
    public List<Permission> getPermissionByRoleId(@RequestParam Long roleId) {
        if (permissionService.getById(roleId )== null){
            throw new RuntimeException("角色不存在");
        }
        return permissionService.getPermissionsByRoleId(roleId);
    }
    @GetMapping("/listPermissionIdsByUserId")
    public Set<String> getPermissionIdsByUserId(@RequestParam Long userId) {
        if (userService.getById(userId)==null){
            throw new RuntimeException("用户不存在");
        }
        return permissionService.getPermissionsByUserIdSet(Set.of(userId));
    }
    @GetMapping("/listPermissionIdsByRoleId")
    public Set<String> getPermissionIdsByRoleId(@RequestParam Long roleId) {
        if (userService.getById(roleId)==null){
            throw new RuntimeException("用户不存在");
        }
        return permissionService.getPermissionsByRoleIdSet(Set.of(roleId));
    }


//    @PostMapping("/delete")
//    public boolean delete(@RequestBody Permission permission) {
//        return permissionService.removeById(permission);
//    }
//    @PostMapping("/delete/ids")
//    private boolean delete(@RequestBody List<Long> ids) {
//        return permissionService.removeByIds(ids);
//    }
}